#!/usr/bin/python
# import Adafruit_CharLCDPlate
# import time
from time import sleep
from Adafruit_CharLCDPlate import Adafruit_CharLCDPlate
from enum import Enum
import RPi.GPIO as GPIO


class Command(Enum):
    UP = 0
    DOWN = 1
    MODE = 2
    ON = 3
    OFF = 4


lcd = Adafruit_CharLCDPlate()

col = (lcd.RED, lcd.YELLOW, lcd.GREEN, lcd.TEAL, lcd.BLUE, lcd.VIOLET,
       lcd.WHITE, lcd.ON, lcd.OFF)

btn = ((lcd.LEFT, 'up click', Command.UP,
        6), (lcd.UP, 'down click', Command.DOWN, 13), (lcd.DOWN, 'mode change',
                                                       Command.MODE, 19),
       (lcd.RIGHT, 'server start', Command.ON, 5), (lcd.SELECT, 'server off',
                                                    Command.OFF, 26))

GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(0, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(5, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(6, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(13, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(19, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(26, GPIO.OUT)
GPIO.output(26, 1)


def get_input():
    while True:
        for b in btn:
            if lcd.buttonPressed(b[0]):
                sleep(0.3)
                print(b[1])
                return b[2]
            if GPIO.input(b[3]) == 0:
                sleep(0.1)
                if GPIO.input(b[3]) == 0:
                    sleep(0.2)
                    print(b[1])
                    return b[2]


def set_color(color):
    lcd.backlight(col[color])


def set_message(s):
    lcd.clear()
    lcd.message(s)


def clear():
    lcd.clear()
